var currentPage = 1;
var nextPage;
var prePage;
var size;
var count;
var booklist;
var pageNumber;
//初始化请求--将当前页和总页面初始化 和 list
$(document).ready(function(){
    pagerRequest(currentPage);
});

//点击按钮发送ajax请求
function pagerRequest(need) {
	$.post("pager", "need=" + need , function(result) {

		size = result['size'];
		count = result['count'];
		booklist = result['booklist'];
		$('#current').text(currentPage);
		var n1;
		var n2;
		n1 = count / size;
		n2 = count % size;
		n1 = Math.floor(n1);
		pageNumber = n1;

		if (n2 !== 0)
			pageNumber = pageNumber + 1;

		$('#total').text(pageNumber);
		putOnTable(booklist);
	});
}

//将数据放在页面上
function putOnTable(list) {
	var table = $('#content');
	$("#content tr:not(:first)").remove();
        
	for ( var k in list) {
		var tr = $("<tr></tr>");
		tr.appendTo(table);
		var book = new Object();
		var td;

		book.bookid = list[k]['bookid'];
		book.bookname = list[k]['bookname'];
		book.authorname = list[k]['authorname'];
		book.publisher = list[k]['publisher'];
		book.pub_date = list[k]['pub_date'];

		td = $("<td>" + book.bookid + "</td>");
		td.appendTo(tr);
		td = $("<td>" + book.bookname + "</td>");
		td.appendTo(tr);
		td = $("<td>" + book.authorname + "</td>");
		td.appendTo(tr);
		td = $("<td>" + book.publisher + "</td>");
		td.appendTo(tr);
		td = $("<td>" + book.pub_date + "</td>");
		td.appendTo(tr);
		td = $("<td><button id=btn"+book.bookid +">借书</button>|<button value=''>查看详情</button>");
		td.appendTo(tr);
		addEvent('btn'+book.bookid,book.bookid);//此处添加事件

	}

}
//点击prepage
$(document).ready(function() {
	$('#prePage').click(function() {
		if (currentPage > 1) {
			currentPage = currentPage - 1;
			pagerRequest(currentPage);
			$('#current').text(currentPage);
			
		} else {
			alert("已至首页");
		}
	});
});
//点击nextpage
$(document).ready(function() {
	$('#nextPage').click(function() {
		if (currentPage < pageNumber) {
			currentPage = currentPage + 1;
			pagerRequest(currentPage);
			$('#current').text(currentPage);
			
		} else {
			alert("已至尾页");
		}
	});
});
//点击jump
$(document).ready(function() {
	$('#jumpBtn').click(function() {
		var topage = $('#jumpPage').val();
		if (topage >= 1 && topage <= pageNumber) {
			currentPage = topage;
			pagerRequest(currentPage);
			$('#current').text(currentPage);
			

		} else {
			alert("跳转不合法!");
		}
	});
});
//点击借书按钮 动态绑定按钮
function addEvent(btnid,bookid){
    $('#'+btnid).bind("click",function(){
        $.post("borrow","bookid="+bookid,function(result){
            var msg ;
            if(result === "false")
                msg = "书本库存不足,或者已经借有";
            else if(result === "success")
                msg = "借书成功";
            pop_init("系统信息",msg);  
            $('#pop_div').fadeIn(400); 
        });
    });
}
//添加通知框
function pop_init(title,content) {  
    //取当前浏览器窗口大小  
    var windowWidth=document.body.clientWidth; 
    var windowHeight=document.body.clientHeight; 
    //弹窗的大小  
    var weight=250;  
    var height=132;  
    $("body").append(  
   "<div id='pop_div'style='display:none;position:absolute;border:1px solid #e0e0e0;z-index:99;width:"+weight+"px;height:"+height+"px;top:"+(windowHeight-height-132)+"px;left:"+(windowWidth-weight)+"px'>"+  
        "<div style='line-height:32px;background:#F4A460;border-bottom:1px solid #e0e0e0;font-size:14px;padding:0 0 0 10px;'>" +  
            "<div style='float:left;'><b>"+title+"</b></div><div style='float:right;cursor:pointer;'><b onclick='pop_close()'>×</b></div>" +  
            "<div style='clear:both'></div>"+  
        "</div>" +  
        "<div id='content' style='background:#EED8AE;height:100px;'>" +  
             content+  
        "</div>"+  
    "</div>" 
    );  
}  
function pop_close(){  
    $('#pop_div').fadeOut(400);  
}  